<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>params</title>
  </head>
  <body>
    <script>
      // 方式一 URLSearchParams对象,考虑版本问题影响是否忽略问号的情况
      const searchParams = new URLSearchParams(window.location.search);
      const a = searchParams.get("a");
      const b = searchParams.get("b");
      console.log(a, b);
      //方式二，通过正则match去转换成数组取 /index.html?a=1&b=2
      const urlParams2 = {};
      const regExp = /[?&]+([^=&]+)=([^&]*)/g;
      const matches = window.location.href.match(regExp);
      matches.forEach((str) => {
        const [key, value] = str.slice(1).split("=");
        urlParams2[key] = value;
      });
      console.log(urlParams2); // {a: '1', b: '2'}
    </script>
  </body>
</html>
